package com.gio.tools.modules.cdp.controller;

import com.gio.tools.common.config.ProjectConfig;
import com.gio.tools.common.utils.DateUtils;
import com.gio.tools.common.utils.GrowingIoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * 使用 DataSource 操作 Hive
 */
@Configuration
@EnableScheduling   // 2.开启定时任务
public class HiveDataSourceController {

    @Autowired
    @Qualifier("hiveDruidDataSource")
    DataSource druidDataSource;
    @Autowired
    private ProjectConfig projectConfig;

    @Scheduled(cron = "0 0 5 * * ?")
    public void timer() throws SQLException, InterruptedException {
        /**
         * dailynew_user发送用户属性
         */
        GrowingIoUtils.queryAndSendCdpUser(
                druidDataSource, projectConfig,
                "select * from gio.dailynew_user where time = " + DateUtils.getYesterday("yyyyMMdd"),
                new String[]{"prov_var-prov_name", "city_var-city_name", "telecom_var-telecom"},
                "uni_key", "0");

        //        String s = "https://rp.jx139.com/gdpTools/api/v1/sendCdpUser?sql=select * from gio.cloudfamily&userIdField=user&varToFields=familyRole_var-familyRole_var,familyUser_var-familyUser_var,familyId_var-familyId_var";
        //        String s = "https://rp.jx139.com/gdpTools/api/v1/sendCdpEvent?sql= select * from gio.cloudfamily limit 1 &isLog=1&userIdField=user&eventKey=newFamilyUser&eventTimeField=effect_date&varToFields=";
        /**
         * gio.cloudfamily_newuser发送用户属性
         */
        GrowingIoUtils.queryAndSendCdpUser(druidDataSource, projectConfig,
                "select * from gio.cloudfamily_newuser where time = " + DateUtils.getYesterday("yyyyMMdd"),
                new String[]{"familyRole_var-familyRole_var", "familyUser_var-familyUser_var", "familyId_var-familyId_var"},
                "user", "0");

        /**
         * gio.cloudfamily_newuser发送用户事件
         */
        GrowingIoUtils.queryAndSendCdpEvent(druidDataSource, projectConfig,
                "select * from gio.cloudfamily_newuser where time = " + DateUtils.getYesterday("yyyyMMdd"),
                new String[]{"familyRole_var-familyRole_var", "familyUser_var-familyUser_var", "familyId_var-familyId_var"},
                "familyJoinUser","user","effect_date", "0");
    }

}